package com.qingmeng.portal.service.impl;


import com.qingmeng.mapper.PhotosMapper;
import com.qingmeng.mapper.UserMapper;
import com.qingmeng.model.Photos;
import com.qingmeng.model.PhotosExample;
import com.qingmeng.model.User;
import com.qingmeng.model.UserExample;
import com.qingmeng.portal.enums.LoginResult;
import com.qingmeng.portal.enums.RegisterResult;
import com.qingmeng.portal.model.vo.UserPersonalInformationVo;
import com.qingmeng.portal.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    public static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PhotosMapper photosMapper;

    /**
     * 用户登录
     * @param username 用户名
     * @param password 密码
     * @return 登录结果
     */
    @Override
    public LoginResult login(String username, String password) {
        //校验用户名
        UserExample userExample = new UserExample();
        userExample.createCriteria().andUsernameEqualTo(username);
        if (userMapper.countByExample(userExample) <= 0) {
            return LoginResult.USERNAME_ERROR;
        }
        //校验密码
        UserExample userExample1 = new UserExample();
        userExample1.createCriteria().andUsernameEqualTo(username).andPasswordEqualTo(password);
        if (userMapper.countByExample(userExample1) <= 0) {
            return LoginResult.PASSWORD_ERROR;
        }
        return LoginResult.LOGIN_SUCCESS;
    }

    /**
     * 用户注册
     *
     *
     * @return 注册结果枚举
     */
    @Override
    public RegisterResult register(String username, String password) {
        //检查用户名是否重复
        UserExample userExample = new UserExample();
        userExample.createCriteria().andUsernameEqualTo(username);
        if (userMapper.countByExample(userExample) > 0) {
            return RegisterResult.USERNAME_EXIST;
        }

        //将信息用户插入User表
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setCreateTime(new Date());
        if (userMapper.insertSelective(user) > 0) {
            return RegisterResult.REG_SUCCESS;
        }
        return RegisterResult.REG_FAILED;
    }

    /**
     * 根据用户id查询个人信息
     *
     * @param id 用户ID
     * @return个人信息
     */
    @Override
    public UserPersonalInformationVo getUserById(Long id) {
        //根据用户id获取user表信息
        User user = userMapper.selectByPrimaryKey(id);
        if (user == null) {
            return null;
        }
        UserPersonalInformationVo userPersonalInformationVo = new UserPersonalInformationVo();
        //将user表中的信息传给userPersonalInformationVo
        if (user.getId() != null)
            userPersonalInformationVo.setId(user.getId());
        if (user.getAge() != null)
        userPersonalInformationVo.setAge(user.getAge());
        if (user.getEmail() != null)
        userPersonalInformationVo.setEmail(user.getEmail());
        if (user.getPersonalProfile() != null)
        userPersonalInformationVo.setPersonalProfile(user.getPersonalProfile());
        if (user.getGender() != null)
        userPersonalInformationVo.setGender(user.getGender());
        if (user.getUsername() != null)
        userPersonalInformationVo.setUsername(user.getUsername());
        if (user.getPhone() != null)
        userPersonalInformationVo.setPhone(user.getPhone());
        if (user.getCreateTime() != null)
        userPersonalInformationVo.setCreateTime(user.getCreateTime());
        //根据用户id获取photos表中对应图片
        PhotosExample photosExample = new PhotosExample();
        photosExample.createCriteria().andUserIdEqualTo(id).andPhotoTypeIdEqualTo(2L);
        List<Photos> photos = photosMapper.selectByExample(photosExample);
        for (Photos photos1 : photos) {
            //将用户头像传给Vo
            userPersonalInformationVo.setUserPhoto(photos1);
        }
        return userPersonalInformationVo;
    }

    /**
     * 根据用户名查询用户信息
     *
     * @param username 用户名
     * @return 用户信息
     */
    @Override
    public UserPersonalInformationVo getUserByUserName(String username) {
        UserPersonalInformationVo userPersonalInformationVo = new UserPersonalInformationVo();
        //根据用户名查询用户信息
        UserExample userExample = new UserExample();
        userExample.createCriteria().andUsernameEqualTo(username);
        List<User> users = userMapper.selectByExample(userExample);
        if (users.size() > 0) {
            for (User user : users){
                //将user表中的信息传给userPersonalInformationVo
                if (user.getId() != null)
                    userPersonalInformationVo.setId(user.getId());
                if (user.getUsername() != null)
                    userPersonalInformationVo.setUsername(user.getUsername());
                if (user.getAge() != null)
                    userPersonalInformationVo.setAge(user.getAge());
                if (user.getEmail() != null)
                    userPersonalInformationVo.setEmail(user.getEmail());
                if (user.getPersonalProfile() != null)
                    userPersonalInformationVo.setPersonalProfile(user.getPersonalProfile());
                if (user.getGender() != null)
                    userPersonalInformationVo.setGender(user.getGender());
                if (user.getPhone() != null)
                    userPersonalInformationVo.setPhone(user.getPhone());
                if (user.getCreateTime() != null)
                    userPersonalInformationVo.setCreateTime(user.getCreateTime());
                //根据用户id获取photos表中对应图片
                PhotosExample photosExample = new PhotosExample();
                photosExample.createCriteria().andUserIdEqualTo(user.getId()).andPhotoTypeIdEqualTo(2L);
                List<Photos> photos = photosMapper.selectByExample(photosExample);
                for (Photos photos1 : photos) {
                    //将用户头像传给Vo
                    userPersonalInformationVo.setUserPhoto(photos1);
                }
                return userPersonalInformationVo;
            }
        }
       return null;
    }

}
